<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

<!--      <el-form-item label="类别编号" prop="code">-->
<!--        <el-input-->
<!--          v-model="queryParams.code"-->
<!--          placeholder="请输入类别编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="类型名称" prop="name">-->
<!--        <el-input-->
<!--          v-model="queryParams.name"-->
<!--          placeholder="请输入类型名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="单位" prop="unit">-->
<!--        <el-input-->
<!--          v-model="queryParams.unit"-->
<!--          placeholder="请输入单位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="价格" prop="price">-->
<!--        <el-input-->
<!--          v-model="queryParams.price"-->
<!--          placeholder="请输入价格"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="售卖价" prop="salePrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.salePrice"-->
<!--          placeholder="请输入售卖价"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="保洁员回收价" prop="cleanerPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.cleanerPrice"-->
<!--          placeholder="请输入保洁员回收价"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="图标" prop="icon">-->
<!--        <el-input-->
<!--          v-model="queryParams.icon"-->
<!--          placeholder="请输入图标"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="串口编号" prop="comNumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.comNumber"-->
<!--          placeholder="请输入串口编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="审核阈值" prop="auditThreshold">-->
<!--        <el-input-->
<!--          v-model="queryParams.auditThreshold"-->
<!--          placeholder="请输入审核阈值"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最后修改时间" prop="lastUpdateTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.lastUpdateTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择最后修改时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最低投递值" prop="minPostingValue">-->
<!--        <el-input-->
<!--          v-model="queryParams.minPostingValue"-->
<!--          placeholder="请输入最低投递值"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="每日计分数量" prop="dailyPostingCount">-->
<!--        <el-input-->
<!--          v-model="queryParams.dailyPostingCount"-->
<!--          placeholder="请输入每日计分数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="类型时间段开启时间" prop="typeOpenTimeArea">-->
<!--        <el-input-->
<!--          v-model="queryParams.typeOpenTimeArea"-->
<!--          placeholder="请输入类型时间段开启时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="背景颜色" prop="backgroundColor">-->
<!--        <el-input-->
<!--          v-model="queryParams.backgroundColor"-->
<!--          placeholder="请输入背景颜色"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['module:typeconfig:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['module:typeconfig:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['module:typeconfig:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['module:typeconfig:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :border="true" :stripe="true" v-loading="loading" :data="typeconfigList" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="图标" align="center" prop="icon" >
        <template slot-scope="scope">
          <img  style="width: 40px;height: 40px" :src="scope.row.icon"/>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="name" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="回收价(元)" align="center" prop="price" />
      <el-table-column label="保洁员回收价(元)" align="center" prop="cleanerPrice" />
      <el-table-column label="售价(元)" align="center" prop="salePrice" />
      <el-table-column label="最低投递值" align="center" prop="minPostingValue" />
      <!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="类别编号" align="center" prop="code" />-->
<!--      <el-table-column label="类型名称" align="center" prop="name" />-->
<!--      <el-table-column label="价格" align="center" prop="price" />-->
<!--      <el-table-column label="串口编号" align="center" prop="comNumber" />-->
<!--      <el-table-column label="审核阈值" align="center" prop="auditThreshold" />-->
<!--      <el-table-column label="最后修改时间" align="center" prop="lastUpdateTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="每日计分数量" align="center" prop="dailyPostingCount" />-->
<!--      <el-table-column label="类型时间段开启时间" align="center" prop="typeOpenTimeArea" />-->
<!--      <el-table-column label="背景颜色" align="center" prop="backgroundColor" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:typeconfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:typeconfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改垃圾分类-配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item  label="类型名称" prop="name">
          <el-input   :disabled="true"  v-model="form.name" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input   :disabled="true" v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="回收价" prop="price">
          <el-input-number v-model="form.price" :precision="2" controls-position="right" @change="handlePrice" :min="0" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="保洁员回收价" prop="cleanerPrice">
          <el-input-number v-model="form.cleanerPrice" :precision="2" controls-position="right" @change="handleCleanerPrice" :min="0" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="出售价" prop="salePrice">
          <el-input-number v-model="form.salePrice" :precision="2" controls-position="right" @change="handleSalePrice" :min="0" :max="10"></el-input-number>
        </el-form-item>

        <el-form-item label="审核阈值" prop="auditThreshold">
          <el-input-number v-model="form.auditThreshold"  controls-position="right" @change="handleAuditThreshold" :min="0" ></el-input-number>
        </el-form-item>
        <el-form-item label="最低投递值" prop="minPostingValue">
          <el-input-number v-model="form.minPostingValue" :precision="3" controls-position="right" @change="handleMinPostingValue"  :min="0" :max="10" ></el-input-number>
         kg
        </el-form-item>
<!--        <el-form-item label="背景颜色" prop="backgroundColor">-->
<!--          <el-input v-model="form.backgroundColor" placeholder="请输入背景颜色" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTypeconfig, getTypeconfig, delTypeconfig, addTypeconfig, updateTypeconfig } from "@/api/module/typeconfig";

export default {
  name: "Typeconfig",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 垃圾分类-配置表格数据
      typeconfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agentId: null,
        code: null,
        name: null,
        unit: null,
        type: null,
        price: null,
        salePrice: null,
        cleanerPrice: null,
        icon: null,
        comNumber: null,
        auditThreshold: null,
        lastUpdateTime: null,
        minPostingValue: null,
        dailyPostingCount: null,
        typeOpenTimeArea: null,
        backgroundColor: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** handleChange 回收价*/
    handlePrice(value){
      this.form.price=value
    },
    handleCleanerPrice(value){
      this.form.cleanerPrice=value
    },
    handleSalePrice(value){
      this.form.salePrice=value
    },
    handleAuditThreshold(value){
      this.form.auditThreshold=value
    },
    handleMinPostingValue(value){
      this.form.auditThreshold=value

    },

    /** 查询垃圾分类-配置列表 */
    getList() {
      this.loading = true;
      var that=this
      listTypeconfig(this.queryParams).then(response => {
        // this.typeconfigList = response.rows;
        response.rows.forEach(item=>{
          item.minPostingValue=item.minPostingValue/1000
          item.salePrice=that.ReduceTwoDigits(item.salePrice)
          item.cleanerPrice=that.ReduceTwoDigits(item.cleanerPrice)
          item.price=that.ReduceTwoDigits(item.price)
        })
        this.typeconfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    ReduceTwoDigits(item){
      item=item/100
      return item
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        agentId: null,
        code: null,
        name: null,
        unit: null,
        type: null,
        price: null,
        salePrice: null,
        cleanerPrice: null,
        icon: null,
        comNumber: null,
        auditThreshold: null,
        lastUpdateTime: null,
        minPostingValue: null,
        dailyPostingCount: null,
        typeOpenTimeArea: null,
        backgroundColor: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加垃圾分类-配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      var that=this
      const id = row.id || this.ids
      getTypeconfig(id).then(response => {
        console.log(response)
        response.data.minPostingValue=response.data.minPostingValue/1000
        response.data.salePrice=that.ReduceTwoDigits(response.data.salePrice)
        response.data.cleanerPrice=that.ReduceTwoDigits(response.data.cleanerPrice)
        response.data.price=that.ReduceTwoDigits(response.data.price)
        this.form = response.data;
        this.open = true;
        this.title = "修改垃圾分类-配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.salePrice=this.form.salePrice*100
            this.form.cleanerPrice=this.form.cleanerPrice*100
            this.form.price=this.form.price*100
            this.form.minPostingValue=this.form.price*1000


            updateTypeconfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTypeconfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除垃圾分类-配置编号为"' + ids + '"的数据项？').then(function() {
        return delTypeconfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/typeconfig/export', {
        ...this.queryParams
      }, `typeconfig_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
